import rclpy
from rclpy.node import Node
from status_interfaces.msg import SystemStatus
import psutil
import platform

class SysStatusPub(Node):
    def __init__(self,node_name):
        super().__init__('sys_status_pub')
        self.pub = self.create_publisher(SystemStatus, 'sys_status', 10)
        self.timer = self.create_timer(1, self.timer_callback)
        self.get_logger().info('sys_status_pub has been started')

    def timer_callback(self):
        msg = SystemStatus()
        msg.stamp = self.get_clock().now().to_msg()
        msg.host_name = platform.node()
        msg.cpu_percent = psutil.cpu_percent()
        msg.memory_percent = psutil.virtual_memory().percent
        msg.memory_total = psutil.virtual_memory().total / 1024 / 1024
        msg.memory_available = psutil.virtual_memory().available / 1024 / 1024
        msg.net_recv = psutil.net_io_counters().bytes_recv / 1024 / 1024
        msg.net_sent = psutil.net_io_counters().bytes_sent / 1024 / 1024
        self.get_logger().info(f'发布：{str(msg)}')
        self.pub.publish(msg)


def main(args=None):
    rclpy.init(args=args)
    node = SysStatusPub()
    rclpy.spin(node)
    rclpy.shutdown()